// C Source File
// Created 11/19/2007; 7:13:00 PM

#include "Civilization.h"
#include "Map.h"
#include "Game.h"
#include "Terrain.h"
#include "Technology.h"
#include "Constants.h"

char civNumb = CIV_NUMBER;

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AllocateCivData ////////////////////////////////////////////////////////////////////////////////////////////////
// allocates all civ data /////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool AllocateCivData(int number)
{
	civNumb = number;

	civilization = (Civ*)Malloc(civNumb * sizeof(Civ));
	if(civilization == NULL) return 0;
	
	aiCivs = (AI*)Malloc((civNumb-1) * sizeof(AI));
	if(aiCivs == NULL) return 0;
	
	int i;
	for(i = 0; i < civNumb; i++)
	{
		civilization[i].inter.isKnown = (bool*)Malloc(civNumb * sizeof(bool));
		civilization[i].inter.relation = (char*)Malloc(civNumb * sizeof(char));
		civilization[i].fog = (BitArray*)Malloc((game.mapWidth*game.mapHeight/8+1)*sizeof(char));
		if(civilization[i].fog == NULL) return 0;
	}
	
	for(i = 0; i < civNumb-1; i++)
	{	aiCivs[i].attitude = (char*)Malloc(civNumb * sizeof(char)); }

	return 1;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// FreeCivData ////////////////////////////////////////////////////////////////////////////////////////////////////
// frees all civ data /////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void FreeCivData()
{
	int i;
	if(civilization != NULL)
	{
		for(i = 0; i < civNumb; i++)
		{
			Delete(civilization[i].inter.isKnown);
			Delete(civilization[i].inter.relation);
			Delete(civilization[i].fog);
		}
		Free(civilization); civilization = NULL;
	}
	
	if(aiCivs != NULL)
	{
		for(i = 0; i < civNumb-1; i++)
			Delete(aiCivs[i].attitude);
		Free(aiCivs); aiCivs = NULL;
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// NewCiv /////////////////////////////////////////////////////////////////////////////////////////////////////////
// creates a new civilization /////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void NewCiv(char index)
{
	char type = -1;
	char loop;
	
	if(index == 0)
	{
		#ifdef DEBUG
		type = 0;
		#else
		type = GetCivType();
		#endif
	}
	else
	{
		while(type == -1)
		{
			type = random(CIV_TYPES);
			for(loop = 0; loop < index; loop++)
				if(type == civilization[loop].type)
				{
					type = -1;
					break;
				}
		}
	}
	
	// set up variables
	civilization[index].gold = 10;
	civilization[index].luxuryPercent = 0;
	civilization[index].sciencePercent = 60;
	
	// set type & name
	civilization[index].type = type;
	civilization[index].currentName = 0;
	
	// set array sizes
	civilization[index].citySize = 0;
	civilization[index].unitSize = 0;
	
	// set technology
	civilization[index].currentTechResearch = -1;
	civilization[index].researchAmount = 0;
	for(loop = 0; loop < TECH_NUMBER/8+1; loop++)
		civilization[index].techs[loop] = 0;
	
	// set fog
	civilization[index].fog = NULL;
		
	// set interactions
	civilization[index].inter.isKnown = (bool*)Malloc(civNumb * sizeof(bool));
	civilization[index].inter.relation = (char*)Malloc(civNumb * sizeof(char));
	for(loop = 0; loop < civNumb; loop++)
	{
		if(loop == index)
		{
			civilization[index].inter.isKnown[loop] = 1;
			civilization[index].inter.relation[loop] = 1;
		}
		else
		{
			civilization[index].inter.isKnown[loop] = 0;
			civilization[index].inter.relation[loop] = 0;
		}
	}
}

#define Sqr(X) (X*X)

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// CalculatePopulation ////////////////////////////////////////////////////////////////////////////////////////////
// calculates the population total of all cities (p^2 * 10,000) ///////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int CalculatePopulation()
{
	int total = 0;
	char i;
	for(i = 0; i < civilization[0].citySize; i++)
		total += Sqr(civilization[0].city[i].population);
		
	return total;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// UpdateUnits ////////////////////////////////////////////////////////////////////////////////////////////////////
// updates the units movement and health //////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void UpdateUnits(char civ)
{
	char i;
	for(i = 0; i < civilization[civ].unitSize; i++)
	{
		// regain health
		if(civilization[civ].unit[i].hpCURR < civilization[civ].unit[i].hpFULL)
			civilization[civ].unit[i].hpCURR = 
				min(civilization[civ].unit[i].hpCURR + 10*civilization[civ].unit[i].moveCURR/_units[civilization[civ].unit[i].type].moveMAX, 
						civilization[civ].unit[i].hpFULL);
		
		// settler actions
		civilization[civ].unit[i].moveCURR = _units[civilization[civ].unit[i].type].moveMAX;
		int action = civilization[civ].unit[i].settlerAction;
		
		if(action == 100)
		{
			while(Explore(civ, i) && civilization[civ].unit[i].moveCURR > 0) ;
			if(civilization[civ].unit[i].moveCURR > 0) civilization[civ].unit[i].settlerAction = -1;
		}
		else if(action != -1)
		{
			int t = Worldmap(civilization[civ].unit[i].y-1, civilization[civ].unit[i].x-1)-1;
			
			// for automated settler
			if(action == 10)
			{
				if(civilization[civ].unit[i].settlerActionTime == 0)
				{
					if(civilization[civ].unit[i].x-1 != civilization[civ].unit[i].xTarget ||
					   civilization[civ].unit[i].y-1 != civilization[civ].unit[i].yTarget)
					{
						if(!HasRoad(civilization[civ].unit[i].x-1, civilization[civ].unit[i].y-1))
						{
							action = 0;
						}
						else
						{
							int x[30], y[30];
							int dist = getPath(civilization[civ].unit[i].x-1,     civilization[civ].unit[i].y-1, 
													       civilization[civ].unit[i].xTarget, civilization[civ].unit[i].yTarget, 
													       x, y, civ);
							if(dist == -1)
							{
								civilization[civ].unit[i].xTarget = -1;
								civilization[civ].unit[i].yTarget = -1;
								civilization[civ].unit[i].settlerAction = -1;
								if(civ > 0) aiCivs[civ-1].autoSettlers--;
							}
							else MoveUnit(civ, i, x, y, dist);
							continue;
						}
					}
					else
					{
						if(!HasRoad(civilization[civ].unit[i].x-1, civilization[civ].unit[i].y-1))
							action = 0;
						else if(terrain[t].irrigate && !HasIrrigation(civilization[civ].unit[i].x-1, civilization[civ].unit[i].y-1))
							action = 1;
						else if(terrain[t].mine && !HasMine(civilization[civ].unit[i].x-1, civilization[civ].unit[i].y-1))
							action = 2;
						//else if(!HasRailRoad(civilization[civ].unit[i].x-1, civilization[civ].unit[i].y-1))
						//	action = 3;
						else
						{
							AutomateSettler(civ, i);
							
							if(civilization[civ].unit[i].xTarget != -1)
							{
								int x[30], y[30];
								int dist = getPath(civilization[civ].unit[i].x-1,     civilization[civ].unit[i].y-1, 
														       civilization[civ].unit[i].xTarget, civilization[civ].unit[i].yTarget, 
														       x, y, civ);
								if(dist == -1)
								{
									civilization[civ].unit[i].xTarget = -1;
									civilization[civ].unit[i].yTarget = -1;
									civilization[civ].unit[i].settlerAction = -1;
									if(civ > 0) aiCivs[civ-1].autoSettlers--;
								}
								else MoveUnit(civ, i, x, y, dist);
							}
							else if(civ > 0) aiCivs[civ-1].autoSettlers--;
							continue;
						}
					}
				}
				else
				{
					if(!HasRoad(civilization[civ].unit[i].x-1, civilization[civ].unit[i].y-1))
						action = 0;
					else if(terrain[t].irrigate && !HasIrrigation(civilization[civ].unit[i].x-1, civilization[civ].unit[i].y-1))
						action = 1;
					else if(terrain[t].mine && !HasMine(civilization[civ].unit[i].x-1, civilization[civ].unit[i].y-1))
						action = 2;
					//else if(!HasRailRoad(civilization[civ].unit[i].x-1, civilization[civ].unit[i].y-1))
					//	action = 3;
					//else DebugStr("Error! Method UpdateUnits");
				}
			}
			
			// increase time for action
			civilization[civ].unit[i].settlerActionTime++;
			
			if(action == 0 && civilization[civ].unit[i].settlerActionTime >= terrain[t].movecost)
			{
				BuildRoad(civilization[civ].unit[i].x-1, civilization[civ].unit[i].y-1);
				civilization[civ].unit[i].settlerActionTime = 0;
				if(civilization[civ].unit[i].settlerAction != 10) civilization[civ].unit[i].settlerAction = -1;
			}
			else if(action == 1 && civilization[civ].unit[i].settlerActionTime >= terrain[t].iTurns)
			{
				BuildIrrigation(civilization[civ].unit[i].x-1, civilization[civ].unit[i].y-1);
				civilization[civ].unit[i].settlerActionTime = 0;
				if(civilization[civ].unit[i].settlerAction != 10) civilization[civ].unit[i].settlerAction = -1;
			}
			else if(action == 2 && civilization[civ].unit[i].settlerActionTime >= terrain[t].mTurns)
			{
				BuildMine(civilization[civ].unit[i].x-1, civilization[civ].unit[i].y-1);
				civilization[civ].unit[i].settlerActionTime = 0;
				if(civilization[civ].unit[i].settlerAction != 10) civilization[civ].unit[i].settlerAction = -1;
			}
			else if(action == 3 && civilization[civ].unit[i].settlerActionTime >= terrain[t].movecost)
			{
				BuildRailroad(civilization[civ].unit[i].x-1, civilization[civ].unit[i].y-1);
				civilization[civ].unit[i].settlerActionTime = 0;
				if(civilization[civ].unit[i].settlerAction != 10) civilization[civ].unit[i].settlerAction = -1;
			}
			
			// used up all moves
			civilization[civ].unit[i].moveCURR = 0;
		}
		
		// moving to a target
		if(civilization[civ].unit[i].xTarget != -1 && civilization[civ].unit[i].moveCURR != 0)
		{
			int x[30], y[30];
			int dist = getPath(civilization[civ].unit[i].x-1,     civilization[civ].unit[i].y-1, 
									       civilization[civ].unit[i].xTarget, civilization[civ].unit[i].yTarget, 
									       x, y, civ);
			MoveUnit(civ, i, x, y, dist);
		}
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// UpdateCities ///////////////////////////////////////////////////////////////////////////////////////////////////
// updates the cities production, food... /////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void UpdateCities(char civ)
{
	char i;
	for(i = 0; i < civilization[civ].citySize; i++)
	{
		// Gold
		civilization[civ].gold += civilization[civ].city[i].trade;
		
		// Science
		civilization[civ].researchAmount += civilization[civ].city[i].science;
		
		// Food & Growth
		civilization[civ].city[i].foodStorage += civilization[civ].city[i].food - 2*civilization[civ].city[i].population;
		if(10*(civilization[civ].city[i].population + 1) <= civilization[civ].city[i].foodStorage)
		{
			if(civilization[civ].city[i].population == 8 && !GetBit(civilization[civ].city[i].buildings, 8)) ;
			else if(civilization[civ].city[i].population == 12 && !GetBit(civilization[civ].city[i].buildings, 22)) ;
			else
			{
				if(GetBit(civilization[civ].city[i].buildings, 2) || buildings[39].isConstructed == civ)
					civilization[civ].city[i].foodStorage -= 5*(civilization[civ].city[i].population + 1);
				else
					civilization[civ].city[i].foodStorage -= 10*(civilization[civ].city[i].population + 1);
				GrowCity(civ, i);
			}
		}
		
		// Production
		char p = civilization[civ].city[i].underProduction;
		if(p != -1)
		{
			if(civilization[civ].city[i].underProdType == 2 && p == 37) civilization[civ].researchAmount += 0.5 * civilization[civ].city[i].production;
			else if(civilization[civ].city[i].underProdType == 2 && p == 38) civilization[civ].gold += civilization[civ].city[i].production;
			else civilization[civ].city[i].productionCurr += civilization[civ].city[i].production;
			
			if(civilization[civ].city[i].underProdType == 1)
			{
				if(civilization[civ].city[i].productionCurr >= _units[p].cost)
				{
					if(p == SETTLER)
					{
						if(civilization[civ].city[i].population < 3)
						{
							civilization[civ].city[i].productionCurr = _units[p].cost;
							continue;
						}
						else civilization[civ].city[i].population -= 2;
					}
					civilization[civ].city[i].productionCurr -= _units[p].cost;
					
					if((GetBit(civilization[civ].city[i].buildings, 1) || buildings[46].isConstructed == civ) && _units[p].area == 1)
						NewUnit(civ, p, civilization[civ].city[i].x, civilization[civ].city[i].y, VETERAN);
					else if(GetBit(civilization[civ].city[i].buildings, 33) && _units[p].area == 2)
						NewUnit(civ, p, civilization[civ].city[i].x, civilization[civ].city[i].y, VETERAN);
					else if(GetBit(civilization[civ].city[i].buildings, 31) && _units[p].area == 3)
						NewUnit(civ, p, civilization[civ].city[i].x, civilization[civ].city[i].y, VETERAN);
					else
						NewUnit(civ, p, civilization[civ].city[i].x, civilization[civ].city[i].y, REGULAR);
					if(civ == 0)
					{
						if(ChangeProduction(&game.city_names[civ*240 + i*16], _units[p].name))
						{ 
							ChangeProductionWindow(civ, i); 
							ClrScr(); 
						}
						else civilization[civ].city[i].underProduction = -1;
					}
					else
					{
						AI_UpdateCityProduction(civ, i);
					}
				}
			}
			else
			{
				if(civilization[civ].city[i].productionCurr >= buildings[p].cost)
				{
					AddBuildings(civ, i);
					if(civ == 0)
					{
						if(ChangeProduction(civilization[0].city[i].name, buildings[p].name))
						{ 
							ChangeProductionWindow(civ, i); 
							ClrScr(); 
						}
						else civilization[civ].city[i].underProduction = -1;
					}
					else
					{
						AI_UpdateCityProduction(civ, i);
					}
				}
			}
		}
		else
		{
			if(civ == 0)
			{
				if(ChangeProduction2(civilization[0].city[i].name))
				{ 
					ChangeProductionWindow(civ, i); 
					ClrScr(); 
				}
			}
			else
			{
				AI_UpdateCityProduction(civ, i);
			}
		}
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ShowFoundCiv ///////////////////////////////////////////////////////////////////////////////////////////////////
// shows a little window to inform player they found someone //////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ShowFoundCiv(char civ)
{
	DrawLine( 33, 30, 127, 30, A_NORMAL);
	DrawLine( 33, 60, 127, 60, A_NORMAL);
	DrawLine( 33, 30,  33, 60, A_NORMAL);
	DrawLine(127, 30, 127, 60, A_NORMAL);
	Clr(34, 31, 126, 59);
	
	char s[35];
	sprintf(s, "You have just found: %s,", &game.civ_names[14*civilization[civ].type]);
	DrawStr(35, 32, s, A_NORMAL);
	DrawStr(43, 40, "do you want to trade?", A_NORMAL);
	DrawStr(50, 52, "Yes", A_NORMAL);
	DrawStr(95, 52, "No", A_NORMAL);
	
	char i = 0;
	int key = 0;
	while(key != KEY_ENTER && key != KEY_ESC)
	{
		Highlight2(34+46*i, 49, 46);
		key = ngetchx();
		Highlight2(34+46*i, 49, 46);
		
		if(key == KEY_LEFT)
			{ if(--i < 0) i = 1; }
		else if(key == KEY_RIGHT)
			{ if(++i > 1) i = 0; }
	}
	
	if(key == KEY_ENTER && i == 0)
		TradeWindow(civ);
}

//#define CheckOverlap(X) for(i = 0; i < civNumb; i++) if(!civilization[civ].inter.isKnown[i]) { civilization[civ].inter.isKnown[i] = ((civilization[civ].fog[X] & civilization[i].fog[X]) != 0); civilization[i].inter.isKnown[civ] = civilization[civ].inter.isKnown[i]; if((civ == 0 || i == 0) && civilization[civ].inter.isKnown[i]) ShowFoundCiv(civ); }
void CheckOverlap(char civ, char num)
{ 
	char i;
	for(i = 0; i < civNumb; i++) 
	if(!civilization[civ].inter.isKnown[i]) 
	{ 
		civilization[civ].inter.isKnown[i] = ((civilization[civ].fog[num] & civilization[i].fog[num]) != 0); 
		civilization[i].inter.isKnown[civ] = civilization[civ].inter.isKnown[i]; 
		if((civ == 0 || i == 0) && civilization[civ].inter.isKnown[i]) 
		ShowFoundCiv(civ); 
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// RemoveFog //////////////////////////////////////////////////////////////////////////////////////////////////////
// removes the fog around the position ////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void RemoveFog(char civ, int x, int y)
{
	SetBit(civilization[civ].fog, x + y * game.mapWidth);
	if(x > 0) SetBit(civilization[civ].fog, x-1 + y * game.mapWidth);
	if(x < game.mapWidth - 1) SetBit(civilization[civ].fog, x+1 + y * game.mapWidth);
			
	if(y > 0)
	{
		SetBit(civilization[civ].fog, x + (y-1) * game.mapWidth);
		if(x > 0) 
		{
			SetBit(civilization[civ].fog, x-1 + (y-1) * game.mapWidth);
			CheckOverlap(civ, (x-1 + (y-1) * game.mapWidth)>>3);
		}
		if(x < game.mapWidth - 1) 
		{ 
			SetBit(civilization[civ].fog, x+1 + (y-1) * game.mapWidth);
			CheckOverlap(civ, (x+1 + (y-1) * game.mapWidth)>>3);
		}
	}
	
	if(y < game.mapHeight - 1)
	{
		SetBit(civilization[civ].fog, x + (y+1) * game.mapWidth);
		if(x > 0) 
		{
			SetBit(civilization[civ].fog, x-1 + (y+1) * game.mapWidth);
			CheckOverlap(civ, (x-1 + (y-1) * game.mapWidth)>>3);
		}
		if(x < game.mapWidth - 1) 
		{ 
			SetBit(civilization[civ].fog, x+1 + (y+1) * game.mapWidth);
			CheckOverlap(civ, (x+1 + (y-1) * game.mapWidth)>>3);
		}
	}
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// CalculateIncome ////////////////////////////////////////////////////////////////////////////////////////////////
// sums up all the income from all cities /////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int CalculateIncome(char civ)
{
	int total = 0;
	char i;
	for(i = 0; i < civilization[civ].citySize; i++)
		total += civilization[civ].city[i].trade;
	return total;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// CalculateScience ///////////////////////////////////////////////////////////////////////////////////////////////
// sums up all the science from all cities ////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int CalculateScience(char civ)
{
	int total = 0;
	char i;
	for(i = 0; i < civilization[civ].citySize; i++)
		total += civilization[civ].city[i].science;
	return total;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// RecalculateCities ///////////////////////////////////////////////////////////////////////////////////////////////
// recalculates all the cities in a civ ////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void RecalculateCities(char civ)
{
	char i;
	for(i = 0; i < civilization[civ].citySize; i++)
		CalculateResources(civ, i);
}

void DomesticAdvisor();
void MilitaryAdvisor();
void ForeignAdvisor();
void ScienceAdvisor();

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AdvisorMenu ////////////////////////////////////////////////////////////////////////////////////////////////////
// provides the menu for changing and viewing details of the civilization /////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void AdvisorMenu()
{
	char i = 0;
	int key = 0;
	
	DrawLine( 35, 17, 115, 17, A_NORMAL);
	DrawLine( 35, 71, 115, 71, A_NORMAL);
	DrawLine( 35, 17,  35, 71, A_NORMAL);
	DrawLine(115, 17, 115, 71, A_NORMAL);
	Clr(36, 18, 114, 70);
	DrawLine( 35, 29, 115, 29, A_NORMAL);
	
	FontSetSys(F_8x10);
	DrawStr(45, 19, "Advisors", A_NORMAL);
	FontSetSys(F_6x8);
	
	DrawStr(52, 32, "Domestic", A_NORMAL);
	DrawStr(52, 42, "Military", A_NORMAL);
	DrawStr(55, 52, "Foreign", A_NORMAL);
	DrawStr(55, 62, "Science", A_NORMAL);
	
	FontSetSys(F_4x6);
	
	while(key != KEY_ESC && key != KEY_ENTER)
	{
		Highlight2(36, 30 + i*10, 78);
		key = ngetchx();
		Highlight2(36, 30 + i*10, 78);
		
		if(key == KEY_UP)
		{
			if(--i < 0) i = 3;
		}
		else if(key == KEY_DOWN)
		{
			if(++i > 3) i = 0;
		}
		else if(key == KEY_ESC)
		{
			i = 4;
		}
		else if(key == KEY_ENTER)
		{
			switch(i)
			{
				case 0: DomesticAdvisor(); break;
				case 1: MilitaryAdvisor(); break;
				case 2: ForeignAdvisor(); break;
				case 3: ScienceAdvisor(); break;
			}
		}
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// BuyEmbassyWindow ///////////////////////////////////////////////////////////////////////////////////////////////
// asks if player wants to pay for embassy ////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void BuyEmbassyWindow(char civ)
{
	DrawLine( 40, 30, 120, 30, A_NORMAL);
	DrawLine( 40, 60, 120, 60, A_NORMAL);
	DrawLine( 40, 30,  40, 60, A_NORMAL);
	DrawLine(120, 30, 120, 60, A_NORMAL);
	Clr(41, 31, 119, 59);
	
	char s[25];
	unsigned int cost = civilization[civ].citySize * 15;
	sprintf(s, "embassy for %d gold?", cost);
	DrawStr(42, 32, "Do you want to buy an", A_NORMAL);
	DrawStr(45, 40, s, A_NORMAL);
	DrawStr(50, 52, "Yes", A_NORMAL);
	DrawStr(95, 52, "No", A_NORMAL);
	
	char i = 0;
	int key = 0;
	while(key != KEY_ENTER && key != KEY_ESC)
	{
		Highlight2(41+39*i, 49, 39);
		key = ngetchx();
		Highlight2(41+39*i, 49, 39);
		
		if(key == KEY_LEFT)
			{ if(--i < 0) i = 1; }
		else if(key == KEY_RIGHT)
			{ if(++i > 1) i = 0; }
	}
	
	if(key == KEY_ENTER && i == 0)
	{
		if(civilization[0].gold >= cost)
		{
			civilization[0].gold -= cost;
			civilization[0].inter.isKnown[civ] = 2;
		}
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Advisors ///////////////////////////////////////////////////////////////////////////////////////////////////////
// all the different advisors for the game ////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void DomesticAdvisor()
{
	int key = 0;
	char city = 0, fCity = 0;
	bool update = 1;
	char i;
	char string[35];
	char civ = 0;
	
	while(key != KEY_ESC)
	{
		if(update)
		{
			ClrScr();
			DrawLine(0, 30, 160, 30, A_NORMAL);
			
			sprintf(string, "Income: %d", CalculateIncome(civ));
			DrawStr(3, 3, string, A_NORMAL);
			sprintf(string, "Science: %d", CalculateScience(civ));
			DrawStr(3, 13, string, A_NORMAL);
			
			sprintf(string, "[2nd] Science: %d%%", civilization[civ].sciencePercent);
			DrawStr(92, 3, string, A_NORMAL);
			sprintf(string, "[Shift] Luxury: %d%%", civilization[civ].luxuryPercent);
			DrawStr(90, 13, string, A_NORMAL);
			
			DrawStr(0, 24, "City", A_NORMAL);
			DrawStr(50, 24, "P", A_NORMAL);
			DrawStr(60, 24, "F", A_NORMAL);
			DrawStr(70, 24, "S", A_NORMAL);
			DrawStr(80, 24, "T", A_NORMAL);
			DrawStr(90, 24, "S", A_NORMAL);
			DrawStr(100, 24, "Production", A_NORMAL);
			
			char last = min(civilization[civ].citySize, fCity + 7);
			for(i = fCity; i < last; i++)
			{
				char _i = i - fCity;
				DrawStr(0, 32 + 8*_i, civilization[civ].city[i].name, A_NORMAL);
				
				sprintf(string, "%d", civilization[civ].city[i].population);
				if(civilization[civ].city[i].population < 10) DrawStr(50, 32 + 8*_i, string, A_NORMAL);
				else																  				DrawStr(47, 32 + 8*_i, string, A_NORMAL);
				
				sprintf(string, "%d", civilization[civ].city[i].food);
				if(civilization[civ].city[i].food < 10) DrawStr(60, 32 + 8*_i, string, A_NORMAL);
				else															  		DrawStr(57, 32 + 8*_i, string, A_NORMAL);
				
				sprintf(string, "%d", civilization[civ].city[i].production);
				if(civilization[civ].city[i].production < 10) DrawStr(70, 32 + 8*_i, string, A_NORMAL);
				else													  					 		DrawStr(67, 32 + 8*_i, string, A_NORMAL);
				
				sprintf(string, "%d", civilization[civ].city[i].trade);
				if(civilization[civ].city[i].trade < 10 && civilization[0].city[i].trade > -1) DrawStr(80, 32 + 8*_i, string, A_NORMAL);
				else											  						 																			 DrawStr(77, 32 + 8*_i, string, A_NORMAL);
				
				sprintf(string, "%d", civilization[civ].city[i].science);
				if(civilization[civ].city[i].science < 10) DrawStr(90, 32 + 8*_i, string, A_NORMAL);
				else							  											 DrawStr(87, 32 + 8*_i, string, A_NORMAL);
				
				if(civilization[civ].city[i].underProdType == 1)
					sprintf(string, "%d%% - %s", 100 * civilization[civ].city[i].productionCurr / _units[civilization[civ].city[i].underProduction].cost, _units[civilization[civ].city[i].underProduction].name);
				else
					sprintf(string, "%d%% - %s", 100 * civilization[civ].city[i].productionCurr / buildings[civilization[civ].city[i].underProduction].cost, buildings[civilization[civ].city[i].underProduction].name);
				DrawStr(100, 32 + 8*_i, string, A_NORMAL);
			}
			
			//tabs
			DrawStr(1, 93, "Domestic", A_NORMAL);
			DrawStr(35, 93, "Military", A_NORMAL);
			DrawStr(67, 93, "Foreign", A_NORMAL);
			DrawStr(97, 93, "Science", A_NORMAL);
			
			//lines for tabs
			DrawLine(0, 91, 125, 91, A_NORMAL);
			
			DrawLine(  0, 91,   0, 99, A_NORMAL);
			DrawLine( 33, 91,  33, 99, A_NORMAL);
			DrawLine( 65, 91,  65, 99, A_NORMAL);
			DrawLine( 95, 91,  95, 99, A_NORMAL);
			DrawLine(125, 91, 125, 99, A_NORMAL);
			
			for(i = 92; i < 100; i++)
				DrawLine(1, i, 32, i, A_XOR);
			
			update = 0;
			
			#ifdef DEBUG
			sprintf(string, "Civ: %d", civ);
			DrawStr(130, 93, string, A_NORMAL);
			#endif
		}
		
		for (i = 0; i < 8; i++) DrawLine(0, 31 + i + city*8, 160, 31 + i + city*8, A_XOR);
		key = ngetchx();
		for (i = 0; i < 8; i++) DrawLine(0, 31 + i + city*8, 160, 31 + i + city*8, A_XOR);
		
		if(key == KEY_UP)
		{
			if(--city < 0)
			{
				if(fCity > 0)
				{
					char dif = (fCity > 2 ? 3 : fCity);
					city += dif;
					fCity -= dif;
					update = 1;
				}
				else city = 0;
			}
		}
		else if(key == KEY_DOWN)
		{
			if(++city + fCity > civilization[0].citySize-1) 
			{
				city--;
			}
			else if(city > 6)
			{
				char dif = (fCity + 10 > civilization[0].citySize ? civilization[0].citySize - 7 - fCity : 3);
				city -= dif;
				fCity += dif;
				update = 1;
			}
		}
		#ifdef DEBUG
		else if(key == KEY_LEFT)
		{
			if(--civ < 0) civ = civNumb - 1;
			update = 1;
		}
		else if(key == KEY_RIGHT)
		{
			if(++civ > civNumb - 1) civ = 0;
			update = 1;
		}
		#endif
		else if(key == _2ND_UP)
		{
			civilization[civ].sciencePercent+=10;
			if(civilization[civ].sciencePercent > 100) civilization[civ].sciencePercent = 100;
			if(civilization[civ].sciencePercent + civilization[civ].luxuryPercent > 100) civilization[civ].luxuryPercent-=10;
			RecalculateCities(civ);
			update = 1;
		}
		else if(key == _2ND_DOWN)
		{
			civilization[civ].sciencePercent-=10;
			if(civilization[civ].sciencePercent < 0) civilization[civ].sciencePercent = 0;
			RecalculateCities(civ);
			update = 1;
		}
		else if(key == SHIFT_UP)
		{
			civilization[civ].luxuryPercent+=10;
			if(civilization[civ].luxuryPercent > 100) civilization[civ].luxuryPercent = 100;
			if(civilization[civ].sciencePercent + civilization[civ].luxuryPercent > 100) civilization[civ].sciencePercent-=10;
			RecalculateCities(civ);
			update = 1;
		}
		else if(key == SHIFT_DOWN)
		{
			civilization[civ].luxuryPercent-=10;
			if(civilization[civ].luxuryPercent < 0) civilization[civ].luxuryPercent = 0;
			RecalculateCities(civ);
			update = 1;
		}
		else if(key == KEY_ENTER)
		{
			ViewCity(civ, city);
			update = 1;
		}
		else if(key == KEY_F2)
		{
			MilitaryAdvisor();
			return;
		}
		else if(key == KEY_F3)
		{
			ForeignAdvisor();
			return;
		}
		else if(key == KEY_F4)
		{
			ScienceAdvisor();
			return;
		}
	}
}

void MilitaryAdvisor()
{
	int key = 0;
	char city = 0, fCity = 0;
	bool update = 1;
	char i;
	char string[35];
	char civ = 0;
	
	while(key != KEY_ESC)
	{
		if(update)
		{
			ClrScr();
			
			//tabs
			DrawStr( 1, 93, "Domestic", A_NORMAL);
			DrawStr(35, 93, "Military", A_NORMAL);
			DrawStr(67, 93, "Foreign",  A_NORMAL);
			DrawStr(97, 93, "Science",  A_NORMAL);
			
			//lines for tabs
			DrawLine(0, 91, 125, 91, A_NORMAL);
			
			DrawLine(  0, 91,   0, 99, A_NORMAL);
			DrawLine( 33, 91,  33, 99, A_NORMAL);
			DrawLine( 65, 91,  65, 99, A_NORMAL);
			DrawLine( 95, 91,  95, 99, A_NORMAL);
			DrawLine(125, 91, 125, 99, A_NORMAL);
			
			for(i = 92; i < 100; i++)
				DrawLine(34, i, 64, i, A_XOR);
			
			update = 0;
		}
		
		key = ngetchx();
		
		if(key == KEY_LEFT)
		{
			if(--civ < 0) civ = civNumb - 1;
			update = 1;
		}
		else if(key == KEY_RIGHT)
		{
			if(++civ > civNumb - 1) civ = 0;
			update = 1;
		}
		else if(key == KEY_ENTER)
		{
		}
		else if(key == KEY_F1)
		{
			DomesticAdvisor();
			return;
		}
		else if(key == KEY_F3)
		{
			ForeignAdvisor();
			return;
		}
		else if(key == KEY_F4)
		{
			ScienceAdvisor();
			return;
		}
	}
}

void ForeignAdvisor()
{
	int key = 0;
	char city = 0, fCity = 0;
	bool update = 1;
	char i;
	char string[35];
	char civ = 0;
	
	while(key != KEY_ESC)
	{
		if(update)
		{
			ClrScr();
			
			DrawStr(5,5,"?:Unknown  *:Cease-fire  +:Peace  -:War", A_NORMAL);
			
			DrawStr(5, 20, "Civilizations", A_NORMAL);
			DrawLine(4, 27, 49, 27, A_NORMAL);
			
			DrawStr(61, 20, "Relations", A_NORMAL);
			DrawLine(60, 27, 95, 27, A_NORMAL);
			
			DrawStr(107, 20, "Technology", A_NORMAL);
			DrawLine(106, 27, 147, 27, A_NORMAL);
			
			DrawLine(57, 23, 52, 23, A_NORMAL);
			DrawLine(57, 23, 55, 21, A_NORMAL);
			DrawLine(57, 23, 55, 25, A_NORMAL);
			
			DrawLine(103, 23,  98, 23, A_NORMAL);
			DrawLine(103, 23, 101, 21, A_NORMAL);
			DrawLine(103, 23, 101, 25, A_NORMAL);
			
			for(i = 0; i < civNumb; i++)
			{
				if(civilization[0].inter.isKnown[i])
				{
					DrawStr(7, 31+i*10, &game.civ_names[14 * civilization[i].type], A_NORMAL);
					
					if(i == civ)
					{
						Highlight2(4, 28+i*10, 45);
						char j, k = 0;
						for(j = 0; j < civNumb; j++)
						{
							if(j != civ)
							{
								if(civilization[0].inter.isKnown[j])
								{
									if(!civilization[i].inter.isKnown[j])
										sprintf(string, "?%s", &game.civ_names[14*civilization[j].type]); // unknown
									else if(civilization[i].inter.relation[j] == CEASE_FIRE)
										sprintf(string, "*%s", &game.civ_names[14*civilization[j].type]); // cease fire
									else if(civilization[i].inter.relation[j] == PEACE)
										sprintf(string, "+%s", &game.civ_names[14*civilization[j].type]); // peace
									else
										sprintf(string, "-%s", &game.civ_names[14*civilization[j].type]); // war
										
									DrawStr(62, 31 + k++ * 10, string, A_NORMAL);
									
									// stuff to do with embassy
									if(i != 0)
									{
										if(buildings[63].isConstructed == 0 || 
											(buildings[48].isConstructed == 0 && !GetBit(civilization[0].techs, buildings[48].endWonder)) || 
											civilization[0].inter.isKnown[i] == EMBASSY)
										{
											char z = 0, dist = 0;
											while(z < TECH_NUMBER && dist < 8)
											{
												if(GetBit(civilization[i].techs, z) && !GetBit(civilization[0].techs, z))
													DrawStr(107, 31 + dist++ * 10, techs[z].name, A_NORMAL);
												z++;
											}
											if(dist == 0) DrawStr(115, 31, "None", A_NORMAL);
										}
										else
										{
											DrawStr(107, 43, "Need Embassy", A_NORMAL);
											if(GetBit(civilization[0].techs, 88))
												DrawStr(111, 53, "MODE to Buy", A_NORMAL);
											else
												DrawStr(107, 53, "Req Writting", A_NORMAL);
										}
									}
									
								}
								else DrawStr(62, 31 + k++ * 10, "Unknown", A_NORMAL);
							}
						}
					}
				}
				else
				{
					DrawStr(7, 31+i*10, "Unknown", A_NORMAL);
					
					if(i == civ)
					{
						Highlight2(4, 28+i*10, 45);
						
						char j;
						for(j = 0; j < civNumb-1; j++)
						{
							DrawStr(62, 31+j*10, "Unknown", A_NORMAL);
						}
					}
				}
			}
			
			//tabs
			DrawStr( 1, 93, "Domestic", A_NORMAL);
			DrawStr(35, 93, "Military", A_NORMAL);
			DrawStr(67, 93, "Foreign",  A_NORMAL);
			DrawStr(97, 93, "Science",  A_NORMAL);
			
			//lines for tabs
			DrawLine(  0, 91, 125, 91, A_NORMAL);
			
			DrawLine(  0, 91,   0, 99, A_NORMAL);
			DrawLine( 33, 91,  33, 99, A_NORMAL);
			DrawLine( 65, 91,  65, 99, A_NORMAL);
			DrawLine( 95, 91,  95, 99, A_NORMAL);
			DrawLine(125, 91, 125, 99, A_NORMAL);
			
			for(i = 92; i < 100; i++)
				DrawLine(66, i, 94, i, A_XOR);
			
			update = 0;
		}
		
		key = ngetchx();
		
		if(key == KEY_DOWN)
		{
			if(++civ > civNumb - 1) civ = 0;
			update = 1;
		}
		else if(key == KEY_UP)
		{
			if(--civ < 0) civ = civNumb - 1;
			update = 1;
		}
		else if(key == KEY_ENTER)
		{
			if(civ != 0)
			{
				TradeWindow(civ);
				update = 1;
			}
		}
		else if(key == KEY_MODE)
		{
			/*#ifdef DEBUG
			char str[50];
			sprintf(str, "%d : %d : %d", civ, civilization[0].inter.isKnown[civ], GetBit(civilization[0].techs, 88));
			ClrScr();
			DrawStr(0,40,str, A_NORMAL);
			ngetchx();
			update = 1;
			#endif*/
			if(civ != 0 && civilization[0].inter.isKnown[civ] == KNOWN && GetBit(civilization[0].techs, 88))
			{
				BuyEmbassyWindow(civ);
				update = 1;
			}
		}
		else if(key == KEY_F1)
		{
			DomesticAdvisor();
			return;
		}
		else if(key == KEY_F2)
		{
			MilitaryAdvisor();
			return;
		}
		else if(key == KEY_F4)
		{
			ScienceAdvisor();
			return;
		}
	}
}

void ScienceAdvisor()
{
	int key = 0;
	char city = 0, fCity = 0;
	bool update = 1;
	char i;
	char string[35];
	char civ = 0;
	
	while(key != KEY_ESC)
	{
		if(update)
		{
			ClrScr();
			
			//tabs
			DrawStr( 1, 93, "Domestic", A_NORMAL);
			DrawStr(35, 93, "Military", A_NORMAL);
			DrawStr(67, 93, "Foreign",  A_NORMAL);
			DrawStr(97, 93, "Science",  A_NORMAL);
			
			//lines for tabs
			DrawLine(0, 91, 125, 91, A_NORMAL);
			
			DrawLine(  0, 91,   0, 99, A_NORMAL);
			DrawLine( 33, 91,  33, 99, A_NORMAL);
			DrawLine( 65, 91,  65, 99, A_NORMAL);
			DrawLine( 95, 91,  95, 99, A_NORMAL);
			DrawLine(125, 91, 125, 99, A_NORMAL);
			
			for(i = 92; i < 100; i++)
				DrawLine(96, i, 124, i, A_XOR);
			
			update = 0;
		}
		
		key = ngetchx();
		
		if(key == KEY_LEFT)
		{
			if(--civ < 0) civ = civNumb - 1;
			update = 1;
		}
		else if(key == KEY_RIGHT)
		{
			if(++civ > civNumb - 1) civ = 0;
			update = 1;
		}
		else if(key == KEY_ENTER)
		{
		}
		else if(key == KEY_F1)
		{
			DomesticAdvisor();
			return;
		}
		else if(key == KEY_F2)
		{
			MilitaryAdvisor();
			return;
		}
		else if(key == KEY_F3)
		{
			ForeignAdvisor();
			return;
		}
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// GetAvailableUnits //////////////////////////////////////////////////////////////////////////////////////////////
// returns all the units available to be created, and its size ////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int GetAvailableUnits(char civ, char unitI[11])
{
	char i, size = 0;
	for(i = 0; i < 19; i++)
	{
		if(_units[i].preq == -1 || GetBit(civilization[civ].techs, _units[i].preq))
		if(_units[i].obsolete == -1 || !GetBit(civilization[civ].techs, _units[i].obsolete))
		{
			unitI[size++] = i;
		}
	}
	return size;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// GetAvailableBuildings //////////////////////////////////////////////////////////////////////////////////////////
// returns all the buildings available to be created, and its size ////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int GetAvailableBuildings(char civ, char city, char buildingI[11])
{
	char i, size = 0;
	for(i = 0; i < 67; i++)
	{
		if(buildings[i].preq == -1 || GetBit(civilization[civ].techs, buildings[i].preq))
		if(buildings[i].endWonder == -1 || !GetBit(civilization[civ].techs, buildings[i].endWonder))
		if(!GetBit(civilization[civ].city[city].buildings, i))
		if(buildings[i].prev == -1 || GetBit(civilization[civ].city[city].buildings, buildings[i].prev))
		if(buildings[i].isConstructed == -1)
		{
			buildingI[size++] = i;
		}
	}
	return size;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TradeWindow ////////////////////////////////////////////////////////////////////////////////////////////////////
// creates trade between the player and the ai ////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void TradeWindow(char civ)
{
	
}
